package cn.ad.server.job.adapter.persistence.job;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Repository;

import cn.ad.server.persistence.dao.SysConfigDao;
import cn.ad.server.domain.SysConfig;
import cn.ad.server.job.domain.job.ConfigRepositoryService;

@Repository
public class ConfigRepositoryServant implements ConfigRepositoryService {

	static final Logger logger = LoggerFactory.getLogger(ConfigRepositoryServant.class);

	@Autowired
	private SysConfigDao sysConfigDao;

	@Resource(name = "cacheRedisTemplate")
	private HashOperations<String, String, String> configHashOperations;

	@Override
	public void warmAllConfig() {
		List<SysConfig> configs = sysConfigDao.findAll();
		if (CollectionUtils.isEmpty(configs))
			return;
		for (SysConfig config : configs) {
			if (config.getStatus() == 1)
				configHashOperations.put(SysConfig.CACHE_NAME, config.getConfigKey(), config.getConfigValue());
			else if (config.getStatus() != 1)
				configHashOperations.delete(SysConfig.CACHE_NAME, config.getConfigKey());
		}
	}
}
